package com.xzsoft.gdwz.services.impl;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xzsoft.gdwz.common.CommonUtil;
import com.xzsoft.gdwz.dao.OrderDao;
import com.xzsoft.gdwz.model.OrderHeader;
import com.xzsoft.gdwz.model.OrderLine;
import com.xzsoft.gdwz.model.TrdPsEmInfLog;
import com.xzsoft.gdwz.services.OrderService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Transactional
@Service("orderService")
public class OrderServiceImpl implements OrderService {
	
	Logger logger = Logger.getLogger(OrderServiceImpl.class);
	
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private CommonUtil commonUtil;
	
	
	@Override
	public void saveOrder(JSONArray jsonArray) throws Exception  {
		logger.info("获取的订单数据："+jsonArray);
		for(int j = 0 ; j<jsonArray.size(); j++){
			JSONObject map = (JSONObject)jsonArray.get(j);
			//订单主信息
			OrderHeader oh = new OrderHeader();
			String inqSource = (String)map.get("INQ_SOURCE");
			if("JD".equals(inqSource)){
				inqSource = "京东采购";
			}else if("SN".equals(inqSource)){
				inqSource = "苏宁采购";
			}else if("DL".equals(inqSource)){
				inqSource = "得力采购";
			}else if("XY".equals(inqSource)){
				inqSource = "西域采购";
			}
			oh.setEcommerceName((String)map.get("INQ_SOURCE"));
			oh.setEcommerceOrderId((String)map.get("INQ_H_ID"));
			oh.setInqHid(UUID.randomUUID().toString());
			oh.setInqHCode((String)map.get("INQ_H_CODE"));
			oh.setInqAmount(map.get("INQ_AMOUNT")+"");
			oh.setCreateDate((String)map.get("CREATE_DATE"));
			oh.setCreateBy((String)map.get("CREATE_BY"));
			oh.setCreateByPhone((String)map.get("CREATE_BY_PHONE"));
			oh.setAddress((String)map.get("ADDRESS"));
			oh.setVendorName((String)map.get("VENDOR_NAME"));
			oh.setVendorPhone((String)map.get("VENDOR_PHONE"));
			try {
				oh = commonUtil.initUserOrgMsg(oh);
				//订单明细信息
				JSONArray array = (JSONArray) map.get("ORDER_LINES");
				//设置订单名称，第一行物资信息+项目单位简称
				if (array.size() > 0) {
					oh.setInqHName((String) ((JSONObject) array.get(0))
							.get("MT_NAME")
							+ "("
							+ oh.getInqHName()
							+ ")-"
							+ inqSource);
				}
				orderDao.insertOrderHeader(oh);
				for (int i = 0; i < array.size(); i++) {
					OrderLine ol = new OrderLine();
					JSONObject jo = (JSONObject) array.get(i);
					ol.setEcommerceName(oh.getEcommerceName());
					ol.setEcommerceSkuId((String) jo.get("CCID_ID"));
					ol.setInqHid(oh.getInqHid());
					ol.setPrName((String) jo.get("MT_NAME"));
					ol.setCcidId(UUID.randomUUID().toString());
					ol.setMtName((String) jo.get("MT_NAME"));
					ol.setMtCode((String) jo.get("MT_CODE"));
					ol.setReqSpec((String) jo.get("REQ_SPEC"));
					ol.setReqModel((String) jo.get("REQ_MODEL"));
					ol.setReqMaterial((String) jo.get("REQ_MATERIAL"));
					ol.setReqUnit((String) jo.get("REQ_UNIT"));
					ol.setReqQuantity(jo.get("REQ_QUANTITY") + "");
					ol.setInqPrice(jo.get("REQ_PRICE") + "");
					ol.setInqAmount(""+ jo.getDouble("REQ_PRICE")*jo.getDouble("REQ_QUANTITY"));
					ol.setReqBrand((String) jo.get("REQ_BRAND"));
					ol.setCreationDate(oh.getCreateDate());
					ol.setCreatedBy((String) map.get("CREATE_BY"));
					ol = commonUtil.initUserOrgMsg(ol);
					orderDao.insertOrderLine(ol);
				}
			} catch (Exception e) {
				e.printStackTrace();
				TrdPsEmInfLog log = new TrdPsEmInfLog();
				log.setId(UUID.randomUUID().toString());
				log.setEmName(inqSource);
				log.setErrorMsg(e.getMessage());
				log.setHasRecieve("0");
				log.setOrderId(oh.getEcommerceOrderId());
				log.setOrderInfo(map.toString());				
				logger.info("错误日志:"+JSONObject.fromObject(log)+"|"+(String)map.get("INQ_H_ID"));
				throw e;
			}
		}
	}

}
